package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.work.modify.ZsSyncAccountDIO;
import com.zmn.oms.zmn.normal.business.interfaces.work.financework.ZsNormalErpFinanceWorkBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类描述：工单订阅监听
 *
 * @author lujia
 * @date 2018/11/30 16:53
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OrderWorkAccountListener {

    private static final Logger logger = LoggerFactory.getLogger(OrderWorkAccountListener.class);


    @Resource
    private ZsNormalErpFinanceWorkBService zsNormalErpFinanceWorkBService;

    private static final String TAG = "erp同步";

    /**
     * 工单算账消息监听
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_ACCOUNT_MQ_TAG)
    public void orderWorkAccount(String message){

        logger.info("#mq#orderworkaccount# recive message:{}", message);

        ZsSyncAccountDIO dio = JSON.parseObject(message, ZsSyncAccountDIO.class);

        try {
            if (StringUtil.isBlank(dio.getLogContent())) {
                dio.setLogContent(dio.getAccountLog());
            }

            zsNormalErpFinanceWorkBService.syncAccount(dio);
        } catch (OmsBaseException oe) {
            logger.error(String.format("#%s task 同步算账结果错误：%s", dio.getWorkId(), oe.getMessage()), oe);
        } catch (Exception e) {
            logger.error(String.format("#%s task 同步算账结果错误：%s", dio.getWorkId(), e.getMessage()), e);
        }
    }

}
